﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AppServidor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            gra = new GraficaMelodia();
            this.leyenda_canales.Image = gra.Leyenda;
            musico = IntrumentoMusical.newInstance();
            this.pictureBox1.Image = gra.Grafica;
            musico.add(gra);
            reproductor = new Thread(new ThreadStart(actualizar_grafica));

            reproductor.Start();
            repositorio = new List<Pista>();
        }
        List<Pista> repositorio;
        Thread reproductor;
        IntrumentoMusical musico;
        int tiempo=5;//min
        int time_refresh = 100;//1/2 seg
        GraficaMelodia gra;


        public Pista getPista(String nombre) {
            foreach (Pista p in repositorio) {

                if (p.Equals(nombre)) {
                    return p;
                }
            }
            return null;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void actualizar_grafica() {

            while(true){
                gra.finalizar();
                this.pictureBox1.Image = gra.Grafica;
                Thread.Sleep(time_refresh);
            }
        }
        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (!rbtn_pista.Checked)
            {
                //compilar
                AnalizadorLista interprete = new AnalizadorLista();
                interprete.isValid(txtSource.Text, new sintacticoListas());
                List<Lista> listas = interprete.bibliotecaListas;
                this.listBox2.Items.Clear();
                foreach (Lista aux in listas)
                    this.listBox2.Items.Add(aux);
            }
            else {

                CompiladorPista compi = new CompiladorPista();
                Pista p=compi.getPista(txtSource.Text);
                List<Simbolo> ts = compi.TablaSimbolos;
                if (p != null)
                {

                    if (ts.Count>0)
                    {
                        MessageBox.Show("tabla de simbolos con algo");
                        int g;
                        for (g = 0; g < ts.Count; g++) {
                            Simbolo temp = ts[g];
                            richTextBox1.AppendText(temp.Id + "," + temp.Nombre + "," + temp.Valor + "," + temp.Keep + "," + temp.TipoDato + "," + temp.TipoSim + "," + temp.Padre + "," + temp.Ambito);
                        }
                    }
                    else {
                        MessageBox.Show("tabla de simbolos vacia " );
                    }

                    MessageBox.Show("Se compilo correctamente la Pista: " + p.Nombre);
                   
                    ///añadir al reposito
                    p.calcularTiempo();
                    this.repositorio.Add(p);
                }
                else {
                    MessageBox.Show("Error a leer la pista");
                }
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

       
        private void numerar()
        {
            StringBuilder str = new StringBuilder();
            int linemax=0;
           // int linemax = this.textBox1.Lines.Length; 
            for (int i = 1; i <= linemax; i++)
            {
                str.Append(i + "");
                str.Append("\r\n");
            }

            //textBox2.Text = str.ToString();
        }

        private void tabPage2_Click_1(object sender, EventArgs e)
        {

        }

        private void listBox2_MouseDown(object sender, MouseEventArgs e)
        {
          Lista aux= (Lista) this.listBox2.SelectedItem;
          if (aux != null) {
              listBox3.Items.Clear();
              foreach (String pista in aux.Pistas) {
                  listBox3.Items.Add(pista);
              }
          
          }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void setDuracion(int milisegundos) { 
          double segundos=(double)(milisegundos/1000);
          int segundos_ent=(int)segundos;
          double decimas = (segundos - segundos_ent) * 1000;
         
        //  segundos=( segundos * 60);
          this.l_S_seg.Text = String.Format(CultureInfo.InvariantCulture,
                   "{0:00}", segundos) + " seg con " + String.Format(CultureInfo.InvariantCulture,
                   "{0:00.00}", decimas) + " ms";
        }
        private void button3_Click(object sender, EventArgs e)
        {


            new Thread(new ThreadStart(prueba)).Start();
        }




        private void prueba() {

            gra.X_max = 10 * 1000 * 20;//100s
            setDuracion(gra.X_max);
            gra.Y_max = 4500;//Hz

            gra.iniciar();

            for (int j = 0; j < 10; j++)
            {
                int i=6;
                    musico.Reproducir("Si", i, 100, 3);//1
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Si", i, 100, 3);//2
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Do", i + 1, 100, 3);//2.5
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Re", i + 1, 100, 3);//3
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Do", i + 1, 100, 3);//4.5
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Si", i, 100, 3);//5.5
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("La", i, 100, 3);//6.5
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Sol", i, 100, 3);//8
                    System.Threading.Thread.Sleep(20);
                    musico.Reproducir("Sol", i, 100, 3);//8.5
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Si", i, 100, 3);//9
                    System.Threading.Thread.Sleep(10);
                    musico.Reproducir("Do", i, 100, 3);//10
                    System.Threading.Thread.Sleep(10);
            }
            // musico.entonar(IntrumentoMusical.Re);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            Environment.Exit(0);
        }
    }
}
